var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
Page({
    data: {
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        status: {},
        userData: {}
    },
    onLoad: function (options) {},
    // 判断用户信息是否存在 控制登录与个人信息信息显示与隐藏
    onShow: function () {
        let userInfo = wx.getStorageSync('userInfo');
        if (userInfo == '') {
            this.setData({
                hasUserInfo: 0,
            });
        } else {
            this.setData({
                hasUserInfo: 1,
            });
            // 在登陆的情况下获取用户信息
            this.getUserInfo();
        }
        // this.setData({
        //     userInfo: userInfo,
        // });
        wx.removeStorageSync('categoryId');
    },
    // 微信跳转小程序 错误捕捉
    launchAppError: function(e) { 
        console.log(e.detail.errMsg) 
    },
    // 跳转界面至团长申请界面
    goToApply() {
        let res = util.loginNow();
        if (res == true) {
            wx.navigateTo({
                url: '../apply-list/index',
            })
        }
    },
    // 点击拨号商家
    calling() {
        wx.showModal({
            title: '拨号给商家',
            content: '0571-87611806',
            confirmText: '拨号',
            success(res) {
                if (res.confirm) {
                    wx.makePhoneCall({
                        phoneNumber: '057187611806'
                    })
                } else if (res.cancel) {
                    wx.showToast({
                        title: '取消拨号',
                        icon: 'none',
                        duration: 2000
                    })
                }
            }
        })
    },
    // 点击开团跳转开团
    setTeam() {
        let res = util.loginNow();
        if (res == true) {
            wx.navigateTo({
                url: '/pages/ucenter/setteam/setteam',
            });
        }
    },
    // 跳转评论界面
    toRemark: function () {
        let res = util.loginNow();
        if (res == true) {
            // wx.showToast({
            //     title: '火速开发中',
            //     icon: 'success',
            //     duration: 2000
            // })
            wx.navigateTo({
                url: '/pages/ucenter/remark/remark',
            });
        }
    },
    // 跳转团购订单列表
    toTeamOrder() {
        let res = util.loginNow();
        if (res == true) {
            wx.navigateTo({
                url: '../team-order/team-order',
            })
        }
    },
    // 跳转定制订单列表
    toDiyOrder() {
        let res = util.loginNow();
        if (res == true) {
            wx.navigateTo({
                url: '/pages/ucenter/diy-order-list/index',
            });
        }
    },
    // 退款和售后功能
    toRefund() {
        wx.showToast({
            title: '该功能开发中',
            icon: 'success',
            duration: 2000
        })
    },
    // 判断是否登录跳转至订单列表
    toOrderListTap: function (event) {
        let res = util.loginNow();
        if (res == true) {
            let showType = event.currentTarget.dataset.index;
            wx.setStorageSync('showType', showType);
            wx.navigateTo({
                url: '/pages/ucenter/order-list/index',
            });
        }
    },
    // 判断是否登录 跳转至地址列表
    toAddressList: function (e) {
        let res = util.loginNow();
        if (res == true) {
            wx.navigateTo({
                url: '/pages/ucenter/address/index?type=0',
            });
        }
    },
    // 跳转至关于我们
    toAbout: function () {
        wx.navigateTo({
            url: '/pages/ucenter/about/index',
        });
    },
    // 跳转授权登录
    goAuth: function (e) {
        wx.navigateTo({
            url: '/pages/app-auth/index',
        });
    },

    // 获取用户信息
    getUserInfo() {
        let that = this;
        let userID = wx.getStorageSync('userID');
        util.requestApi(api.getUserInfo, {
            userID: userID
        }).then(function (res) {
            if (res.code == 200) {
                that.setData({
                    userData: res.content
                })
            }
        })
    },
    // 下拉刷新
    onPullDownRefresh: function () {
        wx.showNavigationBarLoading()
        this.getUserInfo();
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
    },
    // 跳转至优惠券界面
    goCoupons() {
        let res = util.loginNow();
        if (res == true) {
            wx.navigateTo({
                url: '/pages/ucenter/coupons/coupons',
            });
        }
    },
    // 跳转至优惠券界面
    goVouchers() {
        let res = util.loginNow();
        if (res == true) {
            wx.navigateTo({
                url: '/pages/ucenter/vouchers/index',
            });
        }
    }
})